package com.playball.lottery.customview;

import java.util.ArrayList;
import java.util.HashMap;

import com.playball.lottery.R;
import com.playball.lottery.frm.buyhall.Kuai3ListActivity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class DelButtonAdapterKuai3 extends BaseAdapter {

	private class MyElvSelFive {
		TextView ball;
		TextView ball_money;
		ImageButton buttonClose;
		TextView playtype;
	}


	private ArrayList<HashMap<String, Object>> mAppList;
	private LayoutInflater mInflater;
	private Context mContext;
	private String[] keyString;
	private int[] valueViewID;
	private MyElvSelFive myElv;
	private int playTypeId;

	public DelButtonAdapterKuai3(ArrayList<HashMap<String, Object>> appList) {
		mAppList = appList;
	}

	public DelButtonAdapterKuai3(Context c, ArrayList<HashMap<String, Object>> appList, int resource, String[] from, int[] to,int playTypeid) {
		mAppList = appList;
		mContext = c;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		keyString = new String[from.length];
		valueViewID = new int[to.length];
		playTypeId=playTypeid;
		System.arraycopy(from, 0, keyString, 0, from.length);
		System.arraycopy(to, 0, valueViewID, 0, to.length);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 if ( mContext.getClass().equals(Kuai3ListActivity.class)) {
			if (convertView != null) {
				myElv = (MyElvSelFive) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.buy_new11x5_listnum, null);
				myElv = new MyElvSelFive();
				myElv.ball = (TextView) convertView.findViewById(valueViewID[0]);
				myElv.ball_money = (TextView) convertView.findViewById(valueViewID[1]);
				myElv.buttonClose = (ImageButton) convertView.findViewById(valueViewID[2]);
				myElv.playtype = (TextView) convertView.findViewById(valueViewID[3]);
				convertView.setTag(myElv);
			}

			HashMap<String, Object> appInfo = mAppList.get(position);
			if (appInfo != null) {
				String ball = (String) appInfo.get(keyString[0]);
				String money = (String) appInfo.get(keyString[1]);
				int bid = (Integer) appInfo.get(keyString[2]);
				String playtype = (String) appInfo.get(keyString[3]);
				if(playTypeId==6601){
					ball=ball.replaceAll("111", "3").replaceAll("666", "18");
				}
				myElv.ball.setText(ball);
				myElv.ball_money.setText((Integer.parseInt(money)/2)+"注"+money+"彩豆");
				// myElv.buttonClose.setBackgroundDrawable(myElv.buttonClose.getResources().getDrawable(bid));
				myElv.buttonClose.setOnClickListener(new lvButtonListener(position, myElv.buttonClose.getId()));
				myElv.playtype.setText("["+playtype+"]");
			}

		}
		return convertView;
	}

	class lvButtonListener implements OnClickListener {
		private int position;
		private int btnId;

		lvButtonListener(int pos, int id) {
			position = pos;
			btnId = id;
		}

		@Override
		@SuppressWarnings("all")
		public void onClick(View v) {
			int vid = v.getId();
			if (vid == btnId) {
				removeItem(position);

				if (mContext.getClass().equals(Kuai3ListActivity.class)) {
					((Kuai3ListActivity) mContext).initSimpleAdapter(mAppList);
				}
			}
		}
	}


	@Override
	public int getCount() {
		return mAppList.size();
	}

	@Override
	public Object getItem(int position) {
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void removeItem(int position) {
		//如果用户点击删除操作非常快,可能会有数组下标越界异常
//		Log.v("removeItem", "position= "+position);
//		Log.v("removeItem", "mAppList.size()= "+mAppList.size());
		if(position>=mAppList.size()){
			return;
		}
		mAppList.remove(position);
		this.notifyDataSetChanged();
	}


	public ArrayList<BallDTO> GetElvList() {
		ArrayList<BallDTO> newlist = new ArrayList<BallDTO>();
		//
		for (int i = 0; i < mAppList.size(); i++) {
			String ball = (String) mAppList.get(i).get(keyString[0]);
			String money = (String) mAppList.get(i).get(keyString[1]);
			BallDTO ballDTO = new BallDTO();
			ballDTO.setElv_ball(ball + "-" + money);
			newlist.add(ballDTO);
		}
		return newlist;
	}

	// 判断字符串是否有特点的字符
	static boolean containsAny(String str, String searchChars) {
		return str.contains(searchChars);
	}
}